# python -m project_root.api.api_log
from .api_token import Token 
from util.time_util import timestran
import requests

class Api_Log(Token):


    def get_list(self,username='',start_time='',end_time='',user_agent='',event_type=''):
        url = 'http://hxzhan.rtcloud.com:32111/tm/oplog/list'
        json = {
            'limit':10,
            'page':1,
            'username':username
        }
        if start_time != '':
            json.update(start_time=start_time,end_time=end_time)
        if user_agent != '':
            json.update(user_agent=user_agent)
        if event_type != '':
            json.update(event_type=event_type)
        response = requests.post(url=url,json=json,headers=self.get_header())
        values = []
        keys = ['created_at','username','ip','user_agent','open_id','event_type','content','result']
        for node in response.json()['data']['list']:
            for key in keys:
                if key == 'created_at':
                    values.append(timestran(node[key]))
                elif key == 'user_agent':
                    if node[key] == 'mini':
                        values.append('微信小程序')
                    elif node[key] == 'pc':
                        values.append('浏览器')
                elif key == 'open_id' or key == 'username':
                    if node[key] == '':
                        values.append('-')
                    else:
                        values.append(node[key])
                elif key == 'result':
                    if node[key] == 1:
                        values.append('成功')
                    elif node[key] ==2:
                        values.append('失败')
                elif key == 'content':
                    values.append(node[key].replace('\n',' ').replace(' ',''))
                else:
                    values.append(node[key])
        return values
    
# api = Api_Log()
# print(api.get_list(user_agent='mini'))
        